package cn.wzx.service;

import cn.wzx.entity.Cart;
import cn.wzx.entity.CartBook;
import cn.wzx.mapper.BookMapper;
import cn.wzx.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired(required = false)
    CartMapper cartMapper;

    @Autowired(required = false)
    BookMapper bookMapper;

    // 该用户所有的购物车明细
    @Override
    public List<CartBook> findAll(Integer uid) {
        return cartMapper.findAll(uid);
    }

    // 添加到购物车
    @Override
    public void addCart(Cart cart) {
        // 添加到购物车的次数+1，主页的加入购物车前五显示会重新进行排序
        bookMapper.addFav(cart.getBookid(), 1);
        // 如果该用户的购物车已有相同的书本，则不重复添加到购物车,书本数量+1
        Cart c = cartMapper.findByBookidUid(cart.getUid(), cart.getBookid());
        System.out.println("c================" + c);
        cart.setTcreate(new Date());
        if (c == null){
            cartMapper.addCart(cart);
        }else {
            cartMapper.addNum(c.getId(),1);
        }
    }
}
